package com.yolo.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yolo.common.annotation.Log;
import com.yolo.common.enums.BusinessType;
import com.yolo.entity.Role;
import com.yolo.entity.RoleResource;
import com.yolo.service.RoleResourceService;
import com.yolo.service.RoleService;
import com.yolo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleResourceService roleResourceService;


    @Log(title = "角色模块", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{roleId}")
    public Result roleDelete(@PathVariable("roleId") Integer roleId) {
        return Result.succ(roleService.removeById(roleId));
    }

    @Log(title = "角色模块", businessType = BusinessType.QUERY)
    @GetMapping("/list")
    public Result select(int pageIndex, int pageSize, String roleName) {
        Page<Role> page = new Page<>(pageIndex, pageSize);
        QueryWrapper qw = new QueryWrapper();
        qw.like("role_name", roleName);
        Page<Role> page1 = roleService.page(page, qw);
        return Result.succ(MapUtil.builder()
                .put("count", page1.getTotal())
                .put("data", page1.getRecords())
                .map());
    }

    @Log(title = "角色模块", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result addRole(Role role) {
        roleService.save(role);
        return Result.succ(null);
    }

    @Log(title = "角色模块", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public Result exitRole(String dialogRoleName, String dialogIntroduce, int dialogRoleId) {
        Role role = new Role();
        role.setRoleName(dialogRoleName);
        role.setIntroduce(dialogIntroduce);
        role.setRoleId(dialogRoleId);
        roleService.updateById(role);
        return Result.succ(null);
    }

    @GetMapping("/getResource/{roleId}")
    public Result getResource(@PathVariable("roleId") int roleId) {
        List<Integer> ids = new ArrayList<>();
        List<RoleResource> rs = roleResourceService.list(new QueryWrapper<RoleResource>().eq("role_id", roleId));
        rs.forEach(r -> {
            ids.add(r.getResourceId());
        });
        return Result.succ(ids);
    }

    @Log(title = "角色模块", businessType = BusinessType.GRANT)
    @PostMapping("/batchAddRoleResource")
    public Result Add(String ids, Integer roleId) {
        roleResourceService.remove(new QueryWrapper<RoleResource>().eq("role_id", roleId));
        String[] split = ids.split(",");
        for (String id : split) {
            roleResourceService.save(new RoleResource(roleId, Integer.parseInt(id)));
        }
        return Result.succ("授权成功");
    }

}
